package com.HQCourse.Exception;

import com.HQCourse.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ControllerAdvice aop实现
 * 全局异常补抓
 */
@ControllerAdvice
public class GlobalExceptionHandler {
   @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail(null).message("全局异常Exception");
    }

    /**
     * 特定异常处理
     * 触发特定异常
     * @param zww
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e){
        e.printStackTrace();
        return Result.fail(null).message("执行了ArithmeticException异常");
    }

    /**
     * 自定义异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(HQCourseException.class)
    @ResponseBody
    public Result error(HQCourseException f){
        f.printStackTrace();
        return Result.fail(null).code(f.getCode()).message(f.getMessage());
    }

}
